<?php

namespace app\api\controller\v36;

use app\api\controller\v2\Auth;
use app\common\cachemodel\UserCoupon;
use app\common\cachemodel\zl3\Zl3FortuneYearActiveAwardRecord;
use app\common\cachemodel\ZlCouponAttr;

/**
 * 2022年运活动 抽奖接口
 */
class FortuneYear2022Active extends Auth
{
    /**
     * 获取抽奖结果
     * @return \think\response\Json
     * @throws \Exception
     */
    public function getAward()
    {
        $activeConfig = config('fortune_year_active.2022');

        $awardData = $activeConfig['award_data'];

        $fortuneYearActiveAwardRecordModel = new Zl3FortuneYearActiveAwardRecord();

        $awardNum = $fortuneYearActiveAwardRecordModel->getAwardNum($this->user['id'], 2022);
        // 判断还有没有抽奖机会
        if ($awardNum >= $activeConfig['award_num'])
        {
            return show(config('code.error'), '已完成抽奖，机会已用尽～');
        }
        // 开始抽奖
        $num = random_int(1, 10000000);
        $resScale = 0;
        $resultIndex = null;
        // 获取抽奖结果
        foreach ($awardData as $index => $awardItem)
        {
            $resScale = $resScale + $awardItem['probability'] * 10000000;  // 此次因为要除以100  所以直接*的时候少两个0
            if ($num <= $resScale)
            {
                $resultIndex = $index;
                break;
            }
        }
        // 发放用户优惠券
        $cid = $awardData[$resultIndex]['cid'];
        $userCouponModel = new UserCoupon();
        $couponInfo = (new ZlCouponAttr())->getInfoById($cid, ['desc', 'type', 'rand_min', 'rand_max', 'ur_id', 'gids', 'discount']);
        $cData = [
            'uid' => $this->user['id'],
            'c_id' => $cid,
            'ur_id' => $couponInfo['ur_id'],
            'start_time' => $this->request->time(),
            'end_time' => strtotime("+7 day"),  // 有效期 7 天
            'give_time' => $this->request->time(),
            'status' => 0,
            'price' => $couponInfo['type'] == 4 ? mt_rand($couponInfo['rand_min'], $couponInfo['rand_max']) : 0,
            'is_give' => 0,
            'create_time' => $this->request->time(),
            'update_time' => 0,
        ];
        $ucId = $userCouponModel->insertGetId($cData);
        $userCid = (int)$ucId;
        // 未使用
        $userCouponModel->listDataAdd($this->user['id'], [$userCid => $userCid], 1);
        // 抽奖成功
        // 写入 抽奖记录
        $insertData = [
            'uid' => $this->user['id'],
            'cid' => $cid,
            'ucid' => $userCid,
            'active_id' => 2022,
            'probability' => $awardData[$resultIndex]['probability'],
            'create_time' => $this->request->time(),
        ];
        $res = $fortuneYearActiveAwardRecordModel->SaveData($insertData);
        if ($res)
        {
            return show(config('code.success'), '请求成功', [
                'img' => $awardData[$resultIndex]['img0'],
                'index' => $resultIndex,
                'cname' => $awardData[$resultIndex]['cname'],
                'target' => 'local',
                'link' => 'discount_coupon_center?need_auth=2',
            ]);
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }
}